/**
 * @description User 数据处理层
 * @author malin
 */

const { User } = require('../db/model')

/**
 * 获取用户信息
 * @param {string} userName 
 * @param {string} password 
 */
async function getUserInfo(userName, password) {
    // 查询条件
    let whereOpt = {
        userName
    }
    if (password) {
        whereOpt.password = password
    }
    const result = await User.findOne({
        attributes: ['id', 'userName', 'nickName', 'gender', 'city', 'picture'],
        where: whereOpt
    })
    // console.log('获取用户信息', result)
    if (result) {
        return result.dataValues
    }
    return result
}

async function createUser({ userName, password, nickName, gender = 3 }) {
    const result = await User.create({
        userName,
        password,
        gender,
        nickName: nickName || userName
    })
    // console.log('创建用户', result.dataValues)
    return result.dataValues
}

module.exports = {
    getUserInfo,
    createUser
}
